package cn.ajb.filemanager.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import cn.ajb.filemanager.entity.User;
import cn.ajb.filemanager.service.UserService;
import cn.ajb.filemanager.util.Contant;
import cn.ajb.filemanager.util.md5.Md5Pwd;



/**
 *登录控制类
 * @author zoujing
 *
 */
@Controller
@RequestMapping()
public class LoginController {
	@Autowired
	private UserService userService;
	@Autowired
	private Md5Pwd md5Pwd;
	@Autowired
	private Contant contant;
	/**
	 * 登录请求控制
	 * @param request
	 * @param response
	 * @param model
	 * @param account
	 * @param password
	 * @return
	 */
	@RequestMapping(value="/loginUser")
	public String Login(HttpServletRequest request,HttpServletResponse response,ModelMap model,String account,String password){
		
		if(StringUtils.isNotBlank(account)){
			if(StringUtils.isNotBlank(password)){
				User user=new User();
				user.setAccount(account);
				user.setPassword(md5Pwd.encode(password));
				User findUser = userService.findByAccountAndPassword(user);
				if(findUser!=null){
					//将用户放置到session中
					request.getSession().setAttribute(contant.getUser(),findUser);
					return "menu";
				}else{
					
						model.addAttribute("error", "用户名密码不正确");
						
						return "index";
				
				}
			}else{
				model.addAttribute("error", "密码不能为空");
				
				return "index";
			}
		}else{
			model.addAttribute("error", "账号不能为空");
			
			return "index";
			
			
		}
		
		
		
		
	}
}
